package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.common.MetaConst;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2023-06-29
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {


    public  void  initTableMetaInfoExtra(List<TableMetaInfo>  tableMetaInfoList,String assessDate ){

        //根据tableMetaInfoList  生成辅助信息表  tableMetaInfoExtraList
       List<TableMetaInfoExtra> tableMetaInfoExtraList=new ArrayList<>(tableMetaInfoList.size());
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            TableMetaInfoExtra tableMetaInfoExtra = this.getOne(
                    new QueryWrapper<TableMetaInfoExtra>()
                    .eq("schema_name", tableMetaInfo.getSchemaName()).eq("table_name", tableMetaInfo.getTableName()));
            if(tableMetaInfoExtra==null){
                //创建 保存
                tableMetaInfoExtra=new TableMetaInfoExtra();
                tableMetaInfoExtra.setTableName( tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName( tableMetaInfo.getSchemaName());

                tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);
                tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);

                tableMetaInfoExtra.setLifecycleDays(-1L);

                tableMetaInfoExtra.setDwLevel(getDwLevelByTableName(  tableMetaInfo.getTableName()));

                tableMetaInfoExtra.setCreateTime(new Date());

                tableMetaInfoExtraList.add(tableMetaInfoExtra);

            }

        }
        saveBatch(tableMetaInfoExtraList);


    }


    private  String getDwLevelByTableName(String tableName){
        if(tableName.startsWith("ods")){
            return "ODS";
        } else if (tableName.startsWith("dwd")) {
            return "DWD";
        }else if (tableName.startsWith("dim")) {
            return "DIM";
        }else if (tableName.startsWith("dws")) {
            return "DWS";
        }else if (tableName.startsWith("ads")) {
            return "ADS";
        }else if (tableName.startsWith("dm")) {
            return "DM";
        }else  {
            return "OTHER";
        }

    }

}
